<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>返回字符串中指定文本首次出现的位置 - indexOf()方法</title>
</head>

<body>
    <p id="demo">单击按钮来定位指定文本首次出现的位置。</p>
    <button onclick="myFunction()">点我</button>
    <script>
        function myFunction() {
            var str = "Hello world, welcome to the universe.";
            var n = str.indexOf("welcome");
            document.getElementById("demo").innerHTML = n;
        }
    </script>
</body>

</html>